package com.javalong.affairs.module.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.BackActivity;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.module.main.fragment.AssistantFragment;
import com.javalong.affairs.module.main.fragment.MainFragment;
import com.javalong.affairs.module.main.fragment.NewMsgFragment;
import com.javalong.affairs.module.main.fragment.SettingFragment;
import com.javalong.affairs.module.main.fragment.WorkFragment;
import com.javalong.affairs.module.main.ui.BottomBar;
import com.javalong.affairs.module.userModule.fragment.UpdateUserInfoFragment;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_newmain)
public class NewMainActivity extends BackActivity implements
        BottomBar.OnBottomClick {

    public static void toThisActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, NewMainActivity.class);
        context.startActivity(intent);
    }

    @ViewInject(R.id.ui_toptitle)
    public TopTitle topTitle;
    @ViewInject(R.id.ui_bottombar)
    private BottomBar bottomBar;
    @ViewInject(R.id.ll_root)
    private LinearLayout ll_root;
    private AssistantFragment assistantFragment;
    private MainFragment mainFragment;
    private NewMsgFragment newMsgFragment;
    private SettingFragment settingFragment;
    private WorkFragment workFragment;
    private BaseFragment currFragment;
    private int[] drawables = new int[]{R.drawable.newmain_level_f1,
            R.drawable.newmain_level_f2, R.drawable.newmain_level_f3,
            R.drawable.newmain_level_f4, R.drawable.newmain_level_f5};
    private String[] tips = new String[]{
            "消息", "主界面", "工作", "便捷助手", "设置"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        bottomBar.setOnBottonClick(this);
        bottomBar.setTextColorResId(getResources().getColor(R.color.main_bottom_text_color), getResources().getColor(R.color.main_bottom_text_color_unselected));
        bottomBar.initWithData(drawables, tips);
        loadOneFragment();
    }


    // 载入首视图
    private void loadOneFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (newMsgFragment == null) {
            newMsgFragment = new NewMsgFragment();
        }
        ft.add(R.id.fl_content, newMsgFragment);
        ft.commit();
    }

    @Override
    public void onBottomClick(int index) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        currFragment = null;
        switch (index) {
            case 0:
                if (newMsgFragment == null) {
                    newMsgFragment = new NewMsgFragment();
                }
                currFragment = newMsgFragment;
                break;
            case 1:
                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                }
                currFragment = mainFragment;
                break;
            case 2:
                if (workFragment == null) {
                    workFragment = new WorkFragment();
                }
                currFragment = workFragment;
                break;
            case 3:
                if (assistantFragment == null) {
                    assistantFragment = new AssistantFragment();
                }
                currFragment = assistantFragment;
                break;
            case 4:
                if (settingFragment == null) {
                    settingFragment = new SettingFragment();
                }
                currFragment = settingFragment;
                break;

        }
        ft.replace(R.id.fl_content, currFragment);
        ft.commit();
    }

    @Override
    public void onBackPressed() {
        oneMoreOut();
    }

    public void updateMsgCount(int total) {
        bottomBar.setRedCount(0,total);
    }
}
